(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['chaplin', 'views/base/form_view', 'text!templates/user/form.html', 'lib/number_utils', 'collections/banco_collection'], function(Chaplin, BaseFormView, Template, NumberUtils, BancoCollection) {
    'use strict';
    var UsuarioFormView, _ref;
    return UsuarioFormView = (function(_super) {
      __extends(UsuarioFormView, _super);

      function UsuarioFormView() {
        _ref = UsuarioFormView.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      UsuarioFormView.prototype.template = Template;

      UsuarioFormView.prototype.validationRules = {
        nome: {
          required: true,
          maxlength: 255
        },
        email: {
          email: true,
          maxlength: 255,
          required: true
        },
        username: {
          required: true
        },
        agencia: {
          required: true,
          maxlength: 255
        },
        banco: {
          required: true
        },
        gerente: {
          required: true,
          maxlength: 255
        },
        numeroConta: {
          required: true
        },
        saldo: {
          required: true
        },
        salario: {
          required: true
        }
      };

      UsuarioFormView.prototype.validatorSettings = {
        ignore: ''
      };

      UsuarioFormView.prototype.initialize = function(options) {
        var promise,
          _this = this;
        if (options == null) {
          options = {};
        }
        _.extend(this, options);
        this.bancoCollection = new BancoCollection;
        promise = this.bancoCollection.fetch();
        return promise.done(function() {
          return UsuarioFormView.__super__.initialize.apply(_this, arguments);
        });
      };

      UsuarioFormView.prototype.afterRender = function() {
        UsuarioFormView.__super__.afterRender.apply(this, arguments);
        return this.setupMasks();
      };

      UsuarioFormView.prototype.setupMasks = function() {
        this.DOMElements.$salario.setMask('decimal');
        return this.DOMElements.$saldo.setMask('decimal');
      };

      UsuarioFormView.prototype.getTemplateData = function() {
        return _.extend(UsuarioFormView.__super__.getTemplateData.apply(this, arguments), {
          bancos: _.generateOptions(this.bancoCollection, 'id', 'nome'),
          isNovoUsuario: this.isNovoUsuario
        });
      };

      UsuarioFormView.prototype.cacheDOMElements = function() {
        return _.extend(UsuarioFormView.__super__.cacheDOMElements.apply(this, arguments), {
          $nome: this.$('#nome'),
          $username: this.$('#username'),
          $email: this.$('#email'),
          $agencia: this.$('#agencia'),
          $banco: this.$('#banco'),
          $gerente: this.$('#gerente'),
          $numeroConta: this.$('#numero-conta'),
          $saldo: this.$('#saldo'),
          $salario: this.$('#salario')
        });
      };

      UsuarioFormView.prototype.fillForm = function() {
        this.DOMElements.$nome.val(this.model.get('nome'));
        this.DOMElements.$username.val(this.model.get('usuario'));
        this.DOMElements.$email.val(this.model.get('email'));
        this.DOMElements.$agencia.val(this.model.get('agencia'));
        this.DOMElements.$banco.val(this.model.get('bancoId'));
        this.DOMElements.$gerente.val(this.model.get('gerente'));
        this.DOMElements.$numeroConta.val(this.model.get('numeroConta'));
        this.DOMElements.$saldo.val(this.setDecimal(this.model.get('saldo')));
        return this.DOMElements.$salario.val(this.setDecimal(this.model.get('salario')));
      };

      UsuarioFormView.prototype.fillModel = function() {
        return this.model.set({
          nome: this.DOMElements.$nome.val(),
          usuario: this.DOMElements.$username.val(),
          email: this.DOMElements.$email.val(),
          agencia: this.DOMElements.$agencia.val(),
          bancoId: this.DOMElements.$banco.val(),
          gerente: this.DOMElements.$gerente.val(),
          numeroConta: this.DOMElements.$numeroConta.val(),
          saldo: this.fixDecimal(this.DOMElements.$saldo.val()),
          salario: this.fixDecimal(this.DOMElements.$salario.val())
        });
      };

      UsuarioFormView.prototype.setDecimal = function(val) {
        if (val) {
          return NumberUtils.number_format(val, 2, ',', '.');
        } else {
          return '0,00';
        }
      };

      UsuarioFormView.prototype.fixDecimal = function(val) {
        if (val == null) {
          val = '';
        }
        return val.replace(/\./g, '').replace(',', '.');
      };

      return UsuarioFormView;

    })(BaseFormView);
  });

}).call(this);
